CreateDpyxController = function(){
	
	
	this.initPageData = function(){	
		var transferParam = CommonUtil.takeOutParamObject();
		if(!CommonUtil.isNotBlank(transferParam)){
			dialogWarn("跳转页面参数异常!");
			return;
		}else{		
			//初始化数据到创建活动全局参数变量;
			this.initActivityPublicParam();			
			//渲染页面动态数据;
			this.renderHtmlData();
			//绑定活动日期联动控件;
			this.bindVoucherTimeWidget();	
		 				
		}
		
	};
	
 
	 /**
     * 页面首次加载初始化活动基础数据;
     */
	this.initActivityPublicParam = function(){
		var transferParam = CommonUtil.takeOutParamObject();
		createActivityPublicParam.activityType = transferParam.activityType;
		createActivityPublicParam.voucherStartTime = DateUtil.getDateFormat(new Date());
		createActivityPublicParam.voucherEndTime = DateUtil.getDateFormat(DateUtil.addDays(new Date(),30));
        createActivityPublicParam.goodsInfo = {"goodsName":"","goodsPicId":"","goodsPicUrl":"","localGoodsPicPath":""};
        createActivityPublicParam.voucherRelativeTime = 30;
        createActivityPublicParam.isRelativeTime = true;  
        createActivityPublicParam.autoDelayFlag = 'Y';
        var customerPrice = createActivityPublicParam.prerequisiteData.customerPrice;
        createActivityPublicParam.voucherWorthValue = parseFloat(customerPrice * 0.1).toFixed(2); 
        createActivityPublicParam.voucherMinConsume = parseFloat(customerPrice).toFixed(2);
        if(createActivityPublicParam.voucherWorthValue < 1){
        	createActivityPublicParam.voucherWorthValue = "1.00";
        }
	
	}	
	
	/**
	 * 绑定活动日期联动控件;	
	 */
	this.bindVoucherTimeWidget = function(){	
		ActivityUtil.loadTimeWidget(true,function(){
			var activityDateParams = createActivityPublicParam.selectDateParams;
			activityDateParams.dateType = "voucherDate";
			activityDateParams.pageId = "createDpyxActivityPage";
			activityDateParams.beginDateInputId = "voucher-date-start";
			activityDateParams.endDateInputId = "voucher-date-end";
			activityDateParams.selectBeginDateFunc = function(selectTime){
				if(selectTime != createActivityPublicParam.voucherStartTime){
					createActivityPublicParam.voucherStartTime = selectTime;
					createActivityPublicParam.voucherEndTime = DateUtil.getDateFormat(DateUtil.addDays(DateUtil.str2Date(selectTime),30));
		        	$("#createDpyxActivityPage #voucher-date-end").val("");
		        	ActivityUtil.loadTimeWidget(true);
				}
			};
			activityDateParams.selectEndDateFunc = function(selectTime){
				if(selectTime != createActivityPublicParam.voucherEndTime){
	            	createActivityPublicParam.voucherEndTime = selectTime;
	            	ActivityUtil.loadTimeWidget(true);
				}		
			};
			
		});
	}

   
	/**
	 * 保存券数据;
	 */
	this.saveVoucherData = function(){	
		   if($("#createDpyxActivityPage #quan-deadline").find("span").text()=="相对时间"){
				createActivityPublicParam.isRelativeTime = true;
				createActivityPublicParam.voucherRelativeTime = $("#createDpyxActivityPage #relativeValidDays").val();
				createActivityPublicParam.autoDelayFlag = 'Y';
			}else{
				createActivityPublicParam.isRelativeTime = false;
				createActivityPublicParam.voucherStartTime = $("#createDpyxActivityPage #voucher-date-start").val();
				createActivityPublicParam.voucherEndTime = $("#createDpyxActivityPage #voucher-date-end").val();
				createActivityPublicParam.autoDelayFlag = 'N';
			} 
			createActivityPublicParam.voucherWorthValue = parseFloat($("#createDpyxActivityPage #voucherAmount").val()).toFixed(2);
			createActivityPublicParam.voucherMinConsume = parseFloat($("#createDpyxActivityPage #voucherMinConsumeAmount").val()).toFixed(2);
			createActivityPublicParam.goodsInfo.goodsName =  $("#createDpyxActivityPage #goodsName").val();
			createActivityPublicParam.voucherType = ACTIVITY_TICKET_TYPE.SIG_TICKET;
			
			var otherSetting = createActivityPublicParam.otherSetting;
			otherSetting.voucherWorthValue = createActivityPublicParam.voucherWorthValue;
			otherSetting.voucherName = buildVourcherName(createActivityPublicParam.voucherWorthValue);
			otherSetting.voucherValidateType =(createActivityPublicParam.isRelativeTime)?"RELATIVE":"FIXED";
	} 
	
	this.initDpyxPicData = function(){
		var picElt = "";
		var goodsPicUrl = createActivityPublicParam.goodsInfo.localGoodsPicPath;
	    if(goodsPicUrl!=""){
	    	picElt = "<img src='"+goodsPicUrl+"' alt='商品图片' /><i class='zhk-list_rarrow'></i>";    	
	    }else{
	    	picElt = "非必选项<i class='zhk-list_rarrow'></i>";
	    }
	    $("#createDpyxActivityPage #goodsPic").find("a").html(picElt);
	}
	
	/**
	 * 渲染页面显示;
	 */
	this.renderHtmlData = function(){
		//渲染模版动态数据;
        CommonUtil.render($("#createDpyxActivityPage #dpyxDataContainer"), "dpyx_activity_data_tpl",createActivityPublicParam);	
       	       
	}

	/**
	 * 检查代金券信息;
	 */
	this.checkVoucherData = function(){
		var goodsName = $("#createDpyxActivityPage #goodsName").val();
		if (goodsName != "") {
			if (!(/^[\u4e00-\u9fa5a-zA-Z0-9\-]+$/.test(goodsName))) {
				DialogUtil.dialogWarn("商品名称不允许包含特殊符号!");
				return false;
			}
			if (goodsName.length > 20) {
				DialogUtil.dialogWarn("商品名称不能超过20个字符!");
				return false;
			}
		} else {
			DialogUtil.dialogWarn("请您填写商品名称!");
			return false;
		}
		
		var voucherAmount = $("#createDpyxActivityPage #voucherAmount").val();
		if (voucherAmount == "") {
			DialogUtil.dialogWarn("请您填写券面额!");
			return false;
		}
		if(isNaN(voucherAmount)){
			DialogUtil.dialogWarn('券面额仅允许输入数字');
			return false;
		}
		if (voucherAmount <= 0 || voucherAmount > 999.99) {
			DialogUtil.dialogWarn("券面额必须输入大于0且小于999.99的数字!");
			return false;
		}
		var voucherMinConsumeAmount = $("#createDpyxActivityPage #voucherMinConsumeAmount").val();
		if (voucherMinConsumeAmount == "") {
			DialogUtil.dialogWarn("请填写券消费门槛!");
			return false;
		}
		if(isNaN(voucherMinConsumeAmount)){
			DialogUtil.dialogWarn('券消费门槛金额仅允许输入数字');
			return false;
		}
 
		if (voucherMinConsumeAmount < 0 || voucherMinConsumeAmount > 9999.99) {
			DialogUtil.dialogWarn("券消费门槛金额必须输入大于等于0且小于9999.99的数字!");
			return false;
		}
		if($("#createDpyxActivityPage #quan-deadline").find("span").text()=="相对时间"){
			var relativeDays = $("#createDpyxActivityPage #relativeValidDays").val();
			if (relativeDays <= 0 || relativeDays == "") {
				DialogUtil.dialogWarn("券有效期相对时间最小为1天");
				return false;
			}
			if (!(/^\+?(0|[1-9][0-9]*)$/.test(relativeDays))) {
				DialogUtil.dialogWarn('券有效期相对时间允许输入整天数');
				return false;
			}
			if (relativeDays > 365) {
				DialogUtil.dialogWarn("券有效期相对时间不能大于365天!");
				return false;
			}
		}else{
			var voucherStartTime = $("#createDpyxActivityPage #voucher-date-start").val();
			
			var voucherEndTime = $("#createDpyxActivityPage #voucher-date-end").val();
			
			if(voucherEndTime == ""){
				DialogUtil.dialogWarn('券有效结束日期不能为空!');
				return false;
			}
			
			if(DateUtil.compareDate(voucherStartTime,voucherEndTime)){
     			DialogUtil.dialogWarn("券有效结束日期不能小于券的有效开始日期!");
     		    return false;
     		}
		}
	
		return true;
	}
	
 
    
	
};